package org.example.traffic.trainplan;

import org.example.traffic.organizationadmin.OrganizationAdmin;
import org.example.traffic.trainplan.TrainPlan;
import org.example.traffic.trainplan.TrainPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/train-plans")
public class TrainPlanController {

    @Autowired
    private TrainPlanService trainPlanService;

    // 根据ID获取培训计划
    @GetMapping("/{id}")
    public ResponseEntity<TrainPlan> getTrainPlanById(@PathVariable Integer id) {
        return trainPlanService.getTrainPlanById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    // 创建培训计划
    @PostMapping
    public TrainPlan createTrainPlan(@RequestBody TrainPlan trainPlan) {
        return trainPlanService.createTrainPlan(trainPlan);
    }

    // 更新培训计划
    @PutMapping("/{id}")
    public ResponseEntity<TrainPlan> updateTrainPlan(@PathVariable Integer id, @RequestBody TrainPlan trainPlanDetails) {
        try {
            TrainPlan updatedTrainPlan = trainPlanService.updateTrainPlan(id, trainPlanDetails);
            return ResponseEntity.ok(updatedTrainPlan);
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }
    // 分页查询视频
    @GetMapping
    public ResponseEntity<Page<TrainPlan>> getOrganizations(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size) {
        Page<TrainPlan> organizations = trainPlanService.getTrainPlans(page-1, size);
        return ResponseEntity.ok(organizations);
    }
    // 删除培训计划
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteTrainPlan(@PathVariable Integer id) {
        trainPlanService.deleteTrainPlan(id);
        return ResponseEntity.noContent().build();
    }
}
